package shared.util;

import java.nio.ByteBuffer;

/**
 * Utility class for reading in packets.
 *
 * @author Vincent Tsuei
 * @version 2012-11-28
 */
public class PacketIO {
    
    public static String readString(ByteBuffer b) {
        int length = b.getInt();
        if (length == 0) {
            return "";
        }
        byte[] nameArr = new byte[length];
        b.get(nameArr);
        return new String(nameArr);
    }
    
    public static void putString(ByteBuffer b, String s) {
        b.putInt(s.length());
        if (s.length() > 0) {
            b.put(s.getBytes());
        }
    }
}
